//
//  Player_Vol_Pos_Btn.m
//  ICE_Player
//
//  Created by lijinxin on 11-10-19.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "Player_Vol_Pos_Btn.h"
#import "PlayerViewController.h"

@implementation Player_Vol_Pos_Btn

@synthesize low_y;
@synthesize high_y;
@synthesize is_dragging;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)dealloc {
	[self destroy_SubViews];
    [super dealloc];
}

- (void)create_SubViews:(UIViewController*)controller
{
    [super create_SubViews:controller];
    
    p_controller = (PlayerViewController*)controller;
    is_dragging = NO;
}

- (void)scale_to_fit_iPad
{
    /*CGRect rect = self.bounds;
     CGPoint pnt = {0};
     rect.origin.x = 100.f;
     rect.origin.y = 768.f - 100.f;
     rect.size.width = 1024.f - 100.f - 100.f;
     pnt.x = rect.origin.x + 0.5f * rect.size.width;
     pnt.y = rect.origin.y + 0.5f * rect.size.height;
     self.bounds = rect;
     self.center = pnt;*/
}

- (void) setHidden:(BOOL)enabled
{
    if (enabled)
    {
        CATransition *animation = [CATransition animation];
        animation.delegate = self;
        animation.duration = 0.15f;
        animation.timingFunction = UIViewAnimationCurveEaseInOut;
        animation.fillMode = kCAFillModeForwards;//kCAFillModeBackwards;//
        animation.type = kCATransitionFade;//@"rippleEffect";//110
        //animation.subtype = kCATransitionFromRight;//kCATransitionFromTop;
        //animation.endProgress = 1.0;
        //animation.removedOnCompletion = NO;
        
        //[self.layer addAnimation:animation forKey:@"animation"];
        [self.layer addAnimation:animation forKey:@"animation"];
        
    }
    else
    {
        CATransition *animation = [CATransition animation];
        animation.delegate = self;
        animation.duration = 0.15f;
        animation.timingFunction = UIViewAnimationCurveEaseInOut;
        animation.fillMode = kCAFillModeForwards;//kCAFillModeBackwards;//
        animation.type = kCATransitionFade;//@"rippleEffect";//110
        //animation.subtype = kCATransitionFromRight;//kCATransitionFromTop;
        //animation.endProgress = 1.0;
        //animation.removedOnCompletion = NO;
        
        //[self.layer addAnimation:animation forKey:@"animation"];
        [self.layer addAnimation:animation forKey:@"animation"];
    }
	super.hidden = enabled;
}

- (void)destroy_SubViews
{
}

- (void)set_Btn_Pos:(CGPoint)pt
{
    CGPoint pt_ctr;
    CGRect pt_rct;
    CGFloat pt_h;
    pt.y = pt.y >= low_y ? pt.y : low_y;
    pt.y = pt.y <= high_y ? pt.y : high_y;
    //set center
    pt_ctr = self.center;
    pt_ctr.y = pt.y;
    self.center = pt_ctr;
    //set bounds
    pt_rct = self.bounds;
    pt_h = pt_rct.size.height;
    pt_rct.origin.y = pt.y - (pt_h / 2.f);
    self.bounds = pt_rct;
    
    if (p_controller)
    {
        [((PlayerViewController*)p_controller) On_Drag_Vol_Pos_Bar:((high_y - pt.y) * 100.f / (high_y - low_y))];
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //printf("B");
    is_dragging = YES;
    self.highlighted = YES; 
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //printf("M");
    CGPoint pt = [[touches anyObject] locationInView:self.superview];
    [self set_Btn_Pos:pt];
    //printf("X = %.2f, Y = %.2f\n",pt.x,pt.y);
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    //printf("C");
    CGPoint pt = [[touches anyObject] locationInView:self.superview];
    if (p_controller)
    {
        pt.y = pt.y > high_y ? high_y : (pt.y < low_y ? low_y : pt.y);
        [((PlayerViewController*)p_controller) On_Set_Vol:((high_y - pt.y) * 100.f / (high_y - low_y))];
    }
    is_dragging = NO;
    self.highlighted = NO; 
    [super touchesCancelled:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //printf("E");
    CGPoint pt = [[touches anyObject] locationInView:self.superview];
    if (p_controller)
    {
        pt.y = pt.y > high_y ? high_y : (pt.y < low_y ? low_y : pt.y);
        [((PlayerViewController*)p_controller) On_Set_Vol:((high_y - pt.y) * 100.f / (high_y - low_y))];
    }
    is_dragging = NO;
    self.highlighted = NO; 
    [super touchesEnded:touches withEvent:event];
}



@end
